
/**
 * 正则验证整合
 *
 * @date 2016-01-28
 * @author Silov.yu
 *
 * 参考页面
 * http://jiajun.iteye.com/blog/379748
 * http://www.cnblogs.com/zxin/archive/2013/01/26/2877765.html
 *
 * @param type  正则类型
 * @param text  验证字符串
 * @param minLenth 预设最小长度,为0时为不限制最小长度
 * @param maxLenth 预设最大长度,为0时为不限制最大长度
 * @constructor
 */
function UtilReg(type, text, minLenth, maxLenth)
{
    //参数初始化
    this.type = type;
    this.text = text;
    this.minLength = minLenth;
    this.maxLength = maxLenth;

    //返回值定义
    this.successCode    = 1;
    this.lengthErrCode  = 2;
    this.ruleErrCode    = 3;
}
UtilReg.prototype = {

    regList : {
        'int':/^[-]{0,1}(\d+)$/,  //整数
        'float':/^[-]{0,1}(\d+)[\.]+(\d+)$/, //浮点数
        'number' : /^[-]{0,1}(\d+)[\.]{0,1}(\d*)$/, //数字

        'money':/^\d{1,12}(?:\.\d{1,3})?$/, // money

        'idCard':/^\d{15}$|^\d{18}$|^\d{17}[xX]$/ ,//身份证

        'qq':/^[1-9]\d{4,9}$/,//QQ
        //'pwd':/^[\@A-Za-z0-9\_]{6,16}$/,//密码
        'pwd':/^[\@A-Za-z0-9\_\@\!\#\$\%\&\*]{6,20}$/,//密码

        'mobile':/^((\+86)|(\(\+86\)))?-?(13|14|15|17|18)[0-9]{9}$/, //验证手机号码
        'telephone':/^((\+86)|(\(\+86\)))?-?((0[1,2]{1}\d{1}-?\d{8})|(0[3-9]{1}\d{2}-?\d{7,8}))$/, //验证固定电话
        'phone':/^((\+86)|(\(\+86\)))?-?(13|14|15|17|18)[0-9]{9}$|((\+86)|(\(\+86\)))?-?((0[1,2]{1}\d{1}-?\d{8})|(0[3-9]{1}\d{2}-?\d{7,8}))$/, //手机号码和固定电话
        'zipcode':/^[1-9]\d{5}$/,  //验证邮编

        'email':/^\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, //邮箱
        'chinese':/^[\u4E00-\u9FA5]+$/, //仅汉字
        'char': /^[A-Za-z]+$/,  //仅仅是字母
        'charn':/^[A-Za-z0-9]+$/,  //数字加字母

        'url':/^((http|https|ftp):\/\/)?(\w(\:\w)?@)?([0-9a-z_-]+\.)*?([a-z0-9-]+\.[a-z]{2,6}(\.[a-z]{2})?(\:[0-9]{2,6})?)((\/[^?#<>\/\\*":]*)+(\?[^#]*)?(#.*)?)?$/,

        'realname':/^[a-zA-Z0-9\u4e00-\u9fa5]{1,20}$/ //姓名
    },


    ruleCheck : function(){
        var reg = this.regList[this.type];
        if ((this.minLength > 0 && this.text.length < this.minLength)
            || (this.maxLength > 0 && this.text.length > this.maxLength)) {
            return this.lengthErrCode;
        }

        if (!reg.test(this.text)) {
            return this.ruleErrCode;
        }

        return this.successCode;
    },

    /**
     * 验证个人银行卡号(16 or 19位)
     */
    checkPersonalBankCard : function()
    {
        var bankno = this.text;
        if(bankno == 0||bankno.length == 0){
            return this.lengthErrCode;
        }
        var lastNum=bankno.substr(bankno.length-1,1);//取出最后一位（与luhm进行比较）

        var first15Num=bankno.substr(0,bankno.length-1);//前15或18位
        var newArr=new Array();
        for(var i=first15Num.length-1;i>-1;i--){    //前15或18位倒序存进数组
            newArr.push(first15Num.substr(i,1));
        }
        var arrJiShu=new Array();  //奇数位*2的积 <9
        var arrJiShu2=new Array(); //奇数位*2的积 >9

        var arrOuShu=new Array();  //偶数位数组
        for(var j=0;j<newArr.length;j++){
            if((j+1)%2==1){//奇数位
                if(parseInt(newArr[j])*2<9)
                    arrJiShu.push(parseInt(newArr[j])*2);
                else
                    arrJiShu2.push(parseInt(newArr[j])*2);
            }
            else //偶数位
                arrOuShu.push(newArr[j]);
        }

        var jishu_child1=new Array();//奇数位*2 >9 的分割之后的数组个位数
        var jishu_child2=new Array();//奇数位*2 >9 的分割之后的数组十位数
        for(var h=0;h<arrJiShu2.length;h++){
            jishu_child1.push(parseInt(arrJiShu2[h])%10);
            jishu_child2.push(parseInt(arrJiShu2[h])/10);
        }

        var sumJiShu=0; //奇数位*2 < 9 的数组之和
        var sumOuShu=0; //偶数位数组之和
        var sumJiShuChild1=0; //奇数位*2 >9 的分割之后的数组个位数之和
        var sumJiShuChild2=0; //奇数位*2 >9 的分割之后的数组十位数之和
        var sumTotal=0;
        for(var m=0;m<arrJiShu.length;m++){
            sumJiShu=sumJiShu+parseInt(arrJiShu[m]);
        }

        for(var n=0;n<arrOuShu.length;n++){
            sumOuShu=sumOuShu+parseInt(arrOuShu[n]);
        }

        for(var p=0;p<jishu_child1.length;p++){
            sumJiShuChild1=sumJiShuChild1+parseInt(jishu_child1[p]);
            sumJiShuChild2=sumJiShuChild2+parseInt(jishu_child2[p]);
        }
        //计算总和
        sumTotal=parseInt(sumJiShu)+parseInt(sumOuShu)+parseInt(sumJiShuChild1)+parseInt(sumJiShuChild2);

        //计算Luhm值
        var k= parseInt(sumTotal)%10==0?10:parseInt(sumTotal)%10;
        var luhm= 10-k;

        if(lastNum==luhm){
            return this.successCode;
        }
        else{
            return this.ruleErrCode;
        }
    },
}
